<template>
  <el-dialog :title="title" :visible.sync="open" :width="width" append-to-body>
    <el-upload
      ref="upload"
      :limit="1"
      accept=".zip"
      :headers="upload.headers"
      :action="upload.url"
      :file-list="fileListAdd"
      :on-progress="handleFileUploadProgress"
      :on-success="handleFileSuccess"
      :auto-upload="false"
      drag
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">
        <em>点击</em>或将文件拖拽到这里上传<br />支持扩展名.zip
      </div>
    </el-upload>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitFileForm">保 存</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from "@/utils/auth";

export default {
  props: {
    importData: {
      type: Object,
      default() {
        return {};
      },
    },
    width: {
      type: String,
      default() {
        return "400px";
      },
    },
    title: {
      type: String,
      default() {
        return "导入";
      },
    },
  },
  data() {
    return {
      // 是否显示弹出层（用户导入）
      open: false,
      upload: {},
      fileListAdd: [],
    };
  },
  mounted() {
    this.upload = this.importData;
  },
  methods: {
    /** 显示弹窗*/
    show() {
      this.open = true;
      this.fileListAdd = [];
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.fileListAdd = fileList;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      if (response.code == "200") {
        this.open = false;
        this.$refs.upload.clearFiles();
      } else {
        this.$message.error(response.msg);
      }

      /*     this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
        response.msg +
        "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );*/
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "system/user/importTemplate",
        {},
        `user_template_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog:not(.is-fullscreen) {
  height: auto !important;
}
</style>
